	set more off
	cap log close
	clear
	cap log close
	set matsize 11000

	*Location of files

	*Your directory

global directory ""
*****************************************************************************
global data_use = "$directory/data_use"
global data_use1 = "$directory//data_use/"
global data_clean = "$directory/data_clean"
global analisis = "$directory/analisis"
global logs = "$directory/logs"
global dos = "$directory/dos"
global prog = "$directory/programs"
*****************************************************************************
 
 
 	****************************************************************************************************************************
	**********************************************************CLEAN ACS SAMPLE******************************************
	****************************************************************************************************************************
	
 
 
	/*We are going to build a data that is the same as the ACS but with additional variables that show true 
	receipt of FS, TANF and GA with the true ammount received by the household. */

	log using "$logs/stigma_analysis_acs_probit2_sept2017_iv1.log", replace

	use "$data_use/acsadmin_ny_allyrs.dta", clear

	**Merge using blockid of the ACS
	merge 1:1 cmid svy_yr using "$data_use1/acs_w_tractrate.dta", keep(1 3)

	 drop _merge

	   **Merging distance from tract to local office
	   ren tractid10 tractid_2010

	   ren tractid2000 tractid_2000

	*********Merging distance with RECIPIENTS BLOCKS

   preserve

   keep if svy_yr<=2009

   merge m:1 tractid_2000 using "$data_use1/tract_2000_dist_rec_blocks.dta", keep(1  3)

     drop _merge

    tempfile data1
    
    save `data1'

    restore
    
    preserve

    keep if svy_yr>=2010

    merge m:1 tractid_2010 using "$data_use1/tract_2010_dist_rec_blocks.dta", keep(1 3)

     drop _merge

    tempfile data2

    save `data2'

    restore

           use `data1', clear

     append using `data2'

     
     foreach var of varlist tract_dist_cor tract_dist_dss tract_dist_noep tract_dist {
     ren `var' `var'_rec
     }

*********Merging distance with ALL BLOCKS

	preserve

   keep if svy_yr<=2009

   merge m:1 tractid_2000 using "$data_use1/tract_2000_dist_allblocks.dta", keep(1  3)

     drop _merge

    tempfile data3
    
    save `data3'

    restore
    
    preserve

    keep if svy_yr>=2010

    merge m:1 tractid_2010 using "$data_use1/tract_2010_dist_allblocks.dta", keep(1 3)

     drop _merge

    tempfile data4

    save `data4'

    restore
    
          use `data3', clear

     append using `data4'
     
       foreach var of varlist tract_dist_cor tract_dist_dss tract_dist_noep tract_dist {
     ren `var' `var'_all
     }

*********Merging distance with ALL BLOCKS WEIGHTED BY WEIGHTS PROPPORTIONAL TO RECIPIENT POPULATION

	preserve

   keep if svy_yr<=2009

   merge m:1 tractid_2000 using "$data_use1/tract_2000_dist_allblocks_wrec.dta", keep(1  3)

     drop _merge

    tempfile data5
    
    save `data5'

    restore
    
    preserve

    keep if svy_yr>=2010

    merge m:1 tractid_2010 using "$data_use1/tract_2010_dist_allblocks_wrec.dta", keep(1 3)

     drop _merge

    tempfile data6

    save `data6'

    restore
    
          use `data5', clear

     append using `data6'
     
     foreach p in "F" "PA" {
		foreach var of varlist tract_dist_cor_`p' tract_dist_dss_`p' tract_dist_noep_`p' tract_dist_`p' {
		ren `var' `var'_wrec
		}
	}	

	drop tractid
	gen tractid=tractid_2010 if svy_yr>=2010
	replace tractid=tractid_2000 if svy_yr<=2009
	
	merge m:1 tractid using "$data_use1/tract_demog.dta"
	

gen impute_earn=(fpern!=0)


****
 
gen tract_rate_F=.

gen tract_rate_PA=.


foreach year of numlist 2008(1)2012 {
replace  tract_rate_F= tract_rate_F`year' if svy_yr==`year'
replace  tract_rate_PA= tract_rate_PA`year' if svy_yr==`year'

}



*Creating Survey variables
         
 gen svy_hhrec_F=fs

 label var svy_hhrec_F "Recipient of Food Stamps (ACS)"

 gen svy_hhrec_F_i=fs if ffs==1

 label var svy_hhrec_F "Imputed sample: Recipient of Food Stamps (ACS)"

 gen svy_hhrec_F_ni=fs if ffs==0

 label var svy_hhrec_F "Non-imputed sample: Recipient of Food Stamps (ACS)"

gen F_i=ffs

 
gen svy_hhrec_PA=rec_pa

label var svy_hhrec_PA "Recipient of Public Assistance (ACS)"

gen svy_hhrec_PA_i=rec_pa if pa_i==1

label var svy_hhrec_PA "Imputed sample: Recipient of Public Assistance (ACS)"

gen svy_hhrec_PA_ni=rec_pa if pa_i==0

label var svy_hhrec_PA "Non-imputed sample: Recipient of Public Assistance (ACS)"

ren pa_i PA_i


**we do not have months or ammounts for FS in the ACSOnly ammounts for PA
gen svy_hhamt_F=0

gen svy_hhmth_F=0

gen svy_hhamt_PA=pa

gen svy_hhmth_PA=0


foreach p in "F" "S" "M" "T" "P" {
  ren `p'mnth_ad_rec ad_hhmth_`p'
  ren `p'ad_rec_any ad_hhrec_`p' 
  ren `p'amt_ad_rec ad_hhamt_`p'
  ren `p'ad_rec_now ad_hhrec_now_`p'
  }


gen ad_hhrec_PA=(ad_hhrec_M==1 | ad_hhrec_P==1 | ad_hhrec_S==1 | ad_hhrec_T==1)

egen ad_hhamt_PA=rsum(ad_hhamt_M ad_hhamt_P ad_hhamt_S ad_hhamt_T )

egen ad_hhmth_PA=rmax(ad_hhmth_M ad_hhmth_P ad_hhmth_S ad_hhmth_T )

egen rec_time_PA=rmin(rec_time_T rec_time_P rec_time_M rec_time_S)


        gen FPA_i=(F_i==1 | PA_i==1)




* We need to get the percentage of households in No program, and percentage of households in combination
*of different programs: Doing FS, PA.
*1 "None" 2 "FS" 3 "PA" 4 "FS, PA"

        foreach t in "ad" "svy" {
          gen `t'_hhrec_FPA=1 if  `t'_hhrec_F==0 &  `t'_hhrec_PA==0 
          replace `t'_hhrec_FPA=2 if  `t'_hhrec_F==1 &  `t'_hhrec_PA==0 
          replace `t'_hhrec_FPA=3 if  `t'_hhrec_F==0 &  `t'_hhrec_PA==1 
          replace `t'_hhrec_FPA=4 if  `t'_hhrec_F==1 &  `t'_hhrec_PA==1 
  
}       




        foreach t in "_i" "_ni" {
     gen svy_hhrec_FPA`t'=1 if  svy_hhrec_F`t'==0 &  svy_hhrec_PA`t'==0
 
          replace svy_hhrec_FPA`t'=2 if  svy_hhrec_F`t'==1 &  svy_hhrec_PA`t'==0 
          replace svy_hhrec_FPA`t'=3 if  svy_hhrec_F`t'==0 &  svy_hhrec_PA`t'==1 
          replace svy_hhrec_FPA`t'=4 if  svy_hhrec_F`t'==1 &  svy_hhrec_PA`t'==1 
 
}


tab svy_yr, gen(year_)


        label var year_1 "Year 2008"

        label var year_2 "Year 2009"

        label var year_3 "Year 2010"

        label var year_4 "Year 2011"

        label var year_5 "Year 2012"

        
        

        gen sample1=1

        gen sample2=1 if povindex<2

        
                
        replace povindex=10 if povindex>=10

        gen dpov10=(povindex>=10)

        label var dpov10 "Top coded poverty index (=1)"

        gen district_cd=.									
	replace district_cd=	1	if cty==	1						
	replace district_cd=	2	if cty==	3						
	replace district_cd=	3	if cty==	7						
	replace district_cd=	4	if cty==	9						
	replace district_cd=	5	if cty==	11						
	replace district_cd=	6	if cty==	13						
	replace district_cd=	7	if cty==	15						
	replace district_cd=	8	if cty==	17						
	replace district_cd=	9	if cty==	19						
	replace district_cd=	10	if cty==	21						
	replace district_cd=	11	if cty==	23						
	replace district_cd=	12	if cty==	25						
	replace district_cd=	13	if cty==	27						
	replace district_cd=	14	if cty==	29						
	replace district_cd=	15	if cty==	31						
	replace district_cd=	16	if cty==	33						
	replace district_cd=	17	if cty==	35						
	replace district_cd=	18	if cty==	37						
	replace district_cd=	19	if cty==	39						
	replace district_cd=	20	if cty==	41						
	replace district_cd=	21	if cty==	43						
	replace district_cd=	22	if cty==	45						
	replace district_cd=	23	if cty==	49						
	replace district_cd=	24	if cty==	51						
	replace district_cd=	25	if cty==	53						
	replace district_cd=	26	if cty==	55						
	replace district_cd=	27	if cty==	57						
	replace district_cd=	28	if cty==	59						
	replace district_cd=	66	if cty==	61	| cty==5 |cty==47 | cty==85 |cty==81					
	replace district_cd=	29	if cty==	63						
	replace district_cd=	30	if cty==	65						
	replace district_cd=	31	if cty==	67						
	replace district_cd=	32	if cty==	69						
	replace district_cd=	33	if cty==	71						
	replace district_cd=	34	if cty==	73						
	replace district_cd=	35	if cty==	75						
	replace district_cd=	36	if cty==	77						
	replace district_cd=	37	if cty==	79						
	replace district_cd=	38	if cty==	83						
	replace district_cd=	39	if cty==	87						
	replace district_cd=	40	if cty==	91
							
	replace district_cd=	41	if cty==	93						
	replace district_cd=	42	if cty==	95						
	replace district_cd=	43	if cty==	97						
	replace district_cd=	44	if cty==	99						
	replace district_cd=	45	if cty==	89						
	replace district_cd=	46	if cty==	101						
	replace district_cd=	47	if cty==	103						
	replace district_cd=	48	if cty==	105						
	replace district_cd=	49	if cty==	107						
	replace district_cd=	50	if cty==	109						
	replace district_cd=	51	if cty==	111						
	replace district_cd=	52	if cty==	113						
	replace district_cd=	53	if cty==	115						
	replace district_cd=	54	if cty==	117						
	replace district_cd=	55	if cty==	119						
	replace district_cd=	56	if cty==	121						
	replace district_cd=	57	if cty==	123	
        
        tempfile acsdata

        save `acsdata', replace


        

*******************************FALSE POSITIVES AND FALSE NEGATIVES ANALYSIS********************
        
        
        
        use `acsdata', clear

        
        *generate false positives and false negative rates with all sample and imputed sample
        
                foreach p in F PA {
                  gen falsepos_`p'=1 if ad_hhrec_`p'==0 & svy_hhrec_`p'==1
                  replace falsepos_`p'=0 if ad_hhrec_`p'==0 & svy_hhrec_`p'==0
                  
                gen falsepos_`p'_i=1 if ad_hhrec_`p'==0 & svy_hhrec_`p'==1 & `p'_i==1
                  replace falsepos_`p'_i=0 if ad_hhrec_`p'==0 & svy_hhrec_`p'==0  & `p'_i==1
                  
                gen falseneg_`p'=1 if ad_hhrec_`p'==1 & svy_hhrec_`p'==0
                  replace falseneg_`p'=0 if ad_hhrec_`p'==1 & svy_hhrec_`p'==1
                  
                gen falseneg_`p'_i=1 if ad_hhrec_`p'==1 & svy_hhrec_`p'==0 & `p'_i==1
                  replace falseneg_`p'_i=0 if ad_hhrec_`p'==1 & svy_hhrec_`p'==1  & `p'_i==1
                                
                }


        
        ta mode, gen(mode_)

  
        label var mode_1 "Mode of int.: Mail"

        label var mode_2 "Mode of int.: CATI"

        label var mode_3 "Mode of int.: CAPI"   

        
        **generate monthly amounts
        foreach p in "F" "PA" {
         
        gen ad_hhmamt_`p'=ad_hhamt_`p'/ad_hhmth_`p'
         label var ad_hhmamt_`p' "Monthly amount received for `p'"
         }

        
        ***We are always controlling for:

        #delimit
        local takeup_F "sing0kid singwkid multad0kid hunder18 hover18 male
         age16_29 age30_39 age50_59 age60_69 age_ge70 lths hsgrad colgradplus
         hispanic black other any_emp povindex dpov10 disabled engonly engpoor
  non_cit rural  
         ltrend";
        #delimit cr
        
        #delimit
        global takeup "sing0kid singwkid multad0kid hunder18 hover18 male
         age16_29 age30_39 age50_59 age60_69 age_ge70 lths hsgrad colgradplus
         hispanic black other any_emp povindex dpov10 disabled engonly engpoor
  non_cit rural  
         ltrend";
        #delimit cr
        
        #delimit
        global takeup2 "sing0kid multadwkid multad0kid hunder18 hover18 male
         age16_29 age30_39 age50_59 age60_69 age_ge70 lths hsgrad colgradplus
         hispanic black other any_emp povindex dpov10 disabled engonly engpoor
	non_cit rural  
         ltrend";

        #delimit cr
                
        svyset tractid [w=adjwt], strata(puma) singleunit(scaled)

	gen iv1 = tract_dist_dss_rec
	
	 
	 ******Interactions*********
	 gen fpl2x=(povindex>=2)
	 label var fpl2x "2 x FPL (=1)"
	 
	 gen elderly = (age>=60)
	 
	 
	 global tr_controls "tract_unemployed tract_hispanic tract_black tract_other tract_age16_29 tract_age30_39 tract_age50_59 tract_age60_69 tract_age_ge70 tract_male tract_pov"
	
	save "$data_clean/acs_stigma_data.dta", clear
	
	
	
	****************************************************************************************************************************
	**********************************************************CLEAN CPS SAMPLE******************************************
	****************************************************************************************************************************
	
	/*We are going to build a data that is the same as the CPS but with additional variables that show true 
receipt of FS, TANF and GA with the true ammount received by the household.*/

log using "$logs/stigma_cps.log", replace

	use "$data_use/cps_admin_all.dta", replace
		
	merge 1:1 h_seq h_year using "$data_use1/cps_sample_tracts.dta", 

	drop _merge
	merge 1:1 h_seq h_year using "$data_use1/cps_w_tractrate.dta", 

	drop _merge
	
	
*********Merging distance with RECIPIENTS BLOCKS

   preserve

   keep if h_year<=2009

ren tractid tractid_2000
	
   merge m:1 tractid_2000 using "$data_use1/tract_2000_dist_rec_blocks.dta", keep(1  3)

     drop _merge

    tempfile data1
    
    save `data1'

    restore
    
    preserve

    keep if h_year>=2010
ren tractid tractid_2010

    merge m:1 tractid_2010 using "$data_use1/tract_2010_dist_rec_blocks.dta", keep(1 3)

     drop _merge

    tempfile data2

    save `data2'

    restore

           use `data1', clear

     append using `data2'

     
     foreach var of varlist tract_dist_cor tract_dist_dss tract_dist_noep tract_dist {
     ren `var' `var'_rec
     }

*********Merging distance with ALL BLOCKS

	preserve

   keep if h_year<=2009
   merge m:1 tractid_2000 using "$data_use1/tract_2000_dist_allblocks.dta", keep(1  3)

     drop _merge

    tempfile data3
    
    save `data3'

    restore
    
    preserve

    keep if h_year>=2010

    merge m:1 tractid_2010 using "$data_use1/tract_2010_dist_allblocks.dta", keep(1 3)

     drop _merge

    tempfile data4

    save `data4'

    restore
    
          use `data3', clear

     append using `data4'
     
       foreach var of varlist tract_dist_cor tract_dist_dss tract_dist_noep tract_dist {
     ren `var' `var'_all
     }

*********Merging distance with ALL BLOCKS WEIGHTED BY WEIGHTS PROPPORTIONAL TO RECIPIENT POPULATION

	preserve

  keep if h_year<=2009
    
   merge m:1 tractid_2000 using "$data_use1/tract_2000_dist_allblocks_wrec.dta", keep(1  3)

     drop _merge

    tempfile data5
    
    save `data5'

    restore
    
    preserve

 keep if h_year>=2010
    
    
    merge m:1 tractid_2010 using "$data_use1/tract_2010_dist_allblocks_wrec.dta", keep(1 3)

     drop _merge

    tempfile data6

    save `data6'

    restore
    
          use `data5', clear

     append using `data6'
     
     foreach p in "F" "PA" {
		foreach var of varlist tract_dist_cor_`p' tract_dist_dss_`p' tract_dist_noep_`p' tract_dist_`p' {
		ren `var' `var'_wrec
		}
	}	

	
	gen tractid=tractid_2010 if h_year>=2010
	replace tractid=tractid_2000 if h_year<=2009
	
	merge m:1 tractid using "$data_use1/tract_demog.dta"
	
	
	replace povindex=10 if povindex>=10
	gen dpov10=(povindex>=10)
	label var dpov10 "Top coded poverty index (=1)"
	
	tab h_year, gen(year_)
	label var year_1 "Year 2007"
	label var year_2 "Year 2008"
	label var year_3 "Year 2009"
	label var year_4 "Year 2010"
	label var year_5 "Year 2011"
	label var year_6 "Year 2012"
	
	*generate false positives and false negative rates with all sample and imputed sample
	
		foreach p in F PA H {
		gen falsepos_`p'=1 if ad_hhrec_`p'==0 & svy_hhrec_`p'==1
		replace falsepos_`p'=0 if ad_hhrec_`p'==0 & svy_hhrec_`p'==0
		
		gen falsepos_`p'_i=1 if ad_hhrec_`p'==0 & svy_hhrec_`p'==1 & `p'_i==1
		replace falsepos_`p'_i=0 if ad_hhrec_`p'==0 & svy_hhrec_`p'==0  & `p'_i==1
		
		gen falseneg_`p'=1 if ad_hhrec_`p'==1 & svy_hhrec_`p'==0
		replace falseneg_`p'=0 if ad_hhrec_`p'==1 & svy_hhrec_`p'==1
		
		gen falseneg_`p'_i=1 if ad_hhrec_`p'==1 & svy_hhrec_`p'==0 & `p'_i==1
		replace falseneg_`p'_i=0 if ad_hhrec_`p'==1 & svy_hhrec_`p'==1  & `p'_i==1
				
		}
				
			
		
	****
 
gen tract_rate_F=.
gen tract_rate_PA=.

foreach year of numlist 2008(1)2013 {

local yr1=`year'-1

replace  tract_rate_F= tract_rate_F`yr1' if h_year==`year'
replace  tract_rate_PA= tract_rate_PA`yr1' if h_year==`year'

*replace tract_rate_F=. if tract_rate_F>3
*replace tract_rate_PA=. if tract_rate_PA>3

}

	cap drop flag
	gen flag=substr(tractid,3,3)
	gen flag2=substr(tractid,1,2)
	drop if flag2!="36"
	
	destring flag, gen(cty) force
	gen district_cd=.									
	replace district_cd=	1	if cty==	1						
	replace district_cd=	2	if cty==	3						
	replace district_cd=	3	if cty==	7						
	replace district_cd=	4	if cty==	9						
	replace district_cd=	5	if cty==	11						
	replace district_cd=	6	if cty==	13						
	replace district_cd=	7	if cty==	15						
	replace district_cd=	8	if cty==	17						
	replace district_cd=	9	if cty==	19						
	replace district_cd=	10	if cty==	21						
	replace district_cd=	11	if cty==	23						
	replace district_cd=	12	if cty==	25						
	replace district_cd=	13	if cty==	27						
	replace district_cd=	14	if cty==	29						
	replace district_cd=	15	if cty==	31						
	replace district_cd=	16	if cty==	33						
	replace district_cd=	17	if cty==	35						
	replace district_cd=	18	if cty==	37						
	replace district_cd=	19	if cty==	39						
	replace district_cd=	20	if cty==	41						
	replace district_cd=	21	if cty==	43						
	replace district_cd=	22	if cty==	45						
	replace district_cd=	23	if cty==	49						
	replace district_cd=	24	if cty==	51						
	replace district_cd=	25	if cty==	53						
	replace district_cd=	26	if cty==	55						
	replace district_cd=	27	if cty==	57						
	replace district_cd=	28	if cty==	59						
	replace district_cd=	66	if cty==	61	| cty==5 |cty==47 | cty==85 |cty==81					
	replace district_cd=	29	if cty==	63						
	replace district_cd=	30	if cty==	65						
	replace district_cd=	31	if cty==	67						
	replace district_cd=	32	if cty==	69						
	replace district_cd=	33	if cty==	71						
	replace district_cd=	34	if cty==	73						
	replace district_cd=	35	if cty==	75						
	replace district_cd=	36	if cty==	77						
	replace district_cd=	37	if cty==	79						
	replace district_cd=	38	if cty==	83						
	replace district_cd=	39	if cty==	87						
	replace district_cd=	40	if cty==	91
							
	replace district_cd=	41	if cty==	93						
	replace district_cd=	42	if cty==	95						
	replace district_cd=	43	if cty==	97						
	replace district_cd=	44	if cty==	99						
	replace district_cd=	45	if cty==	89						
	replace district_cd=	46	if cty==	101						
	replace district_cd=	47	if cty==	103						
	replace district_cd=	48	if cty==	105						
	replace district_cd=	49	if cty==	107						
	replace district_cd=	50	if cty==	109						
	replace district_cd=	51	if cty==	111						
	replace district_cd=	52	if cty==	113						
	replace district_cd=	53	if cty==	115						
	replace district_cd=	54	if cty==	117						
	replace district_cd=	55	if cty==	119						
	replace district_cd=	56	if cty==	121						
	replace district_cd=	57	if cty==	123	

	gen county=district_cd
	
	

	**generate monthly amounts
	foreach p in "F" "PA" {
	
	gen ad_hhmamt_`p'=ad_hhamt_`p'/ad_hhmth_`p'
	label var ad_hhmamt_`p' "Monthly amount received for `p'"
	}
	
	***We are always controlling for:
	#delimit
	global takeup "sing0kid singwkid multad0kid hunder18 hover18 
	age16_29 age30_39 age50_59 age60_69 age_ge70 lths hsgrad colgradplus
	hispanic black other any_emp povindex  disabled rural 
	ltrend ";
	#delimit cr
	

	gen iv1 = tract_dist_dss_rec
	
	 
	 
	 ******Interactions*********
	 gen fpl2x=(povindex>=2)
	 label var fpl2x "2 x FPL (=1)"
	 
	 gen elderly = (a_age>=60)
	 
	 global tr_controls "tract_unemployed tract_hispanic tract_black tract_other tract_age16_29 tract_age30_39 tract_age50_59 tract_age60_69 tract_age_ge70 tract_male tract_pov"
	
	gen flag66=(district_cd==66)
	
	cap drop flag*
	gen flag=.
	foreach num of numlist 1(2)57 {
	replace flag=district_cd + 1 if district_cd==`num'
	}
	replace flag=66 if district_cd==66
	replace flag=district_cd if flag==.
	
	replace cty=flag
	
	
		save "$data_clean/cps_stigma_data.dta", clear

	